<?php
namespace Home\Controller;
use Model\LiveSettingModel;
use Model\NewsModel;
use Model\RoomModel;
use Model\SubjectsModel;
use Model\TeacherModel;
use Think\Controller;

class IndexController extends BaseController {

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {

        $tid = I("get.tid");
        $subjectsModel = new SubjectsModel();
        $subjectsList = $subjectsModel->select();
        if(empty($tid)){
            $tid = 0;
            $roomModel = new RoomModel();
            $where['beginTime'] = array("EGT",time());
            $roomList = $roomModel->where($where)->select();
        }else{
            $roomModel = new RoomModel();
            $roomList = $roomModel->table("live_rooms rooms,live_setting setting")->where(" `setting`.`teacherId` = {$tid} AND `rooms`.`beginTime` >=".time()."")->field("rooms.*")->select();
        }

        foreach($roomList as $k=>$v){
            $roomList[$k]['teacher'] = $this->getTeacherMeaage($v['liveUrlId']);
        }
        $newsModel = new NewsModel();
        $news = $newsModel->field("id,image,description,title")->select();
        $this->assign("news",$news);
        $this->assign("tid",$tid);
        $this->assign("roomList",$roomList);
        $this->assign("subjectsList",$subjectsList);
        $userId = cookie("id");
        if(empty($userId)){
            $this->display();
        }else {
            $this->display("userIndex");
        }
    }

    /**
     * @param $liveUrlId
     * @return array
     */
    public function getTeacherMeaage($liveUrlId){
        $teacherModel = new TeacherModel();
        $liveSetting = new LiveSettingModel();
        $settingMessage = $liveSetting->where(" `liveUrlId` = {$liveUrlId} ")->find();

        $teacher = $teacherModel->find($settingMessage['teacherId']);

        switch ($teacher['level'])
        {
            case 1:
                $teacher['levelTitle'] = "特级教师";
                break;
            case 2:
                $teacher['levelTitle'] = "普通教师";
                break;

        }


        return $teacher;
    }
}